#include "KeyLock.h"

namespace game
{

bool KeyLock::isKeyPressed(sf::Keyboard::Key k)
{
	//if a key is pressed
	bool bKeyPressed = false;
	int keySize = _listeningKeys.size();
	for (int i = 0; i < keySize;i++)
	{
		if (sf::Keyboard::isKeyPressed(_listeningKeys.at(i))){bKeyPressed = true;}
	}

	if (bKeyPressed)//if a key is pressed
	{
		if (!sf::Keyboard::isKeyPressed(_lastPressedKey) || _tick > 10)	//New key pressed // or tick timer over//
		{
			if (sf::Keyboard::isKeyPressed(k))
			{
				_lastPressedKey=k;
				_tick = 0;	return true;
			}
		}
	}
	else{	_tick++;}
	return false;
}

bool KeyLock::isMousePressed(sf::Mouse::Button p)
{
	bool bKeyPressed = false;
	int keySize = 6;
	int pk = -1;
	for (int i = 0; i < keySize;i++)
	{
		if (sf::Mouse::isButtonPressed((sf::Mouse::Button)i)){bKeyPressed = true;pk = i;}
	}

	if (bKeyPressed)//if a key is pressed
	{
		if (!sf::Mouse::isButtonPressed(_lastPressedMouseButton) || _mTick > 4)	//40 New key pressed // or tick timer over//
		{
			if (sf::Mouse::isButtonPressed(p))
			{
				_lastPressedMouseButton=p;
				_mTick = 0;	return true;
			}
		}
		else
		{
			_lastPressedMouseButton=(sf::Mouse::Button)pk;
		}
	}
	else{	_mTick++;}
	return false;
}

KeyLock::KeyLock(sf::RenderWindow *w)
{
	_lastPressedKey = sf::Keyboard::A;
	_lastPressedMouseButton = sf::Mouse::Middle;
	_tick = 0;
	_mTick = 0;
	_listeningKeys.push_back(sf::Keyboard::Left);
	_listeningKeys.push_back(sf::Keyboard::Right);
	_listeningKeys.push_back(sf::Keyboard::Up);
	_listeningKeys.push_back(sf::Keyboard::Down);
	_listeningKeys.push_back(sf::Keyboard::Return);
	_listeningKeys.push_back(sf::Keyboard::Back);
	_window = w;
}
 sf::Vector2i KeyLock::getMousePos()
 {
	return sf::Mouse::getPosition(*_window);
 }
}